<!-- 17回复表-16课程交流表-2用户 -->
<template>
    <div class="about">
        <el-form :inline="true" :v-model="queryInfo" class="demo-form-inline">
            <el-form-item label="用户昵称">
                <el-input v-model="queryInfo.nickName" placeholder="请输入关键字" clearable />
            </el-form-item>
            <el-form-item label="问题标题">
                <el-input v-model="queryInfo.title" placeholder="请输入关键字" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getList">搜索</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" style="width: 100%;">
            <el-table-column type="selection" width="50" />
            <el-table-column fixed prop="id" label="编号" width="100" />
             <el-table-column prop="replyText" label="用户回复内容" width="200" />
            <el-table-column prop="createTime" label="创建时间" width="150" />
            <el-table-column prop="user.nickName" label="用户昵称" width="120" />
            <el-table-column prop="coursetalk.title" label="问题标题" width="200"/>
            <el-table-column prop="myReply" label="我的回复" width="200"/>
            <el-table-column fixed="right" label="操作" min-width="150">
                <template #default="scope">
                    <el-button link type="primary" size="small" @click="onEdit(scope.row)">回复</el-button>
                    <el-button link type="primary" @click="onDel(scope.row.id)" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>

    <el-pagination v-model:current-page="queryInfo.pageNum" v-model:page-size="queryInfo.pageSize"
    :page-sizes="[1, 5, 10, 20]" size="default" background="false" layout="total, sizes, prev, pager, next, jumper"
    :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    <el-dialog v-model="saveOrUpdateVisible" :title="saveOrUpdateTitle" width="500" @close="close()">
        <el-form :model="coursereply">
            <el-form-item label="评论内容">
                <el-input v-model="coursereply.replyText" autocomplete="off" />
            </el-form-item>

            <el-form-item label="评价时间">
                <el-date-picker v-model="coursereply.createTime" type="date"/>
            </el-form-item>

            <el-form-item label="用户昵称：">
                {{ coursereply.user.nickName }}
            </el-form-item>

            <el-form-item label="问题标题：">
                {{ coursereply.coursetalk.title }}
            </el-form-item>

            <el-form-item label="我的回复">
                <el-input v-model="coursereply.myReply" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close">
                    取消
                </el-button>
                <el-button type="primary" @click="saveOrUpdate">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
//引入
import { onMounted, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'
const saveOrUpdateVisible = ref(false);
const saveOrUpdateTitle = ref('回复');
const coursereply = ref({
    id: 0,
    replyText: '',
    createTime: '',
    user: {},
    coursetalk: {},
    myReply: '',
})
//修改的页面
const onEdit = (row) => {
    saveOrUpdateVisible.value = true;
    coursereply.value = row;
}
//取消
const close = () => {
    saveOrUpdateVisible.value = false;
    coursereply.value = {
    id: 0,
    replyText: '',
    createTime: '',
    user: {},
    coursetalk: {},
    myReply: '',
}
}
//修改
const saveOrUpdate = () => {
    axios.put("/CourseReply/updateCourseReplyById", coursereply.value)
            .then(resp => {
                console.log(resp.data)
                let result = resp.data;
                if (result.code == 1) {
                    getList();
                    ElMessage({
                        message: '修改成功！',
                        type: 'success',
                    })
                } else {
                    ElMessage({
                        message: '修改失败！',
                        type: 'error',
                    })
                }
            })
    saveOrUpdateVisible.value = false;
    coursereply.value = {
    id: 0,
    replyText: '',
    createTime: '',
    user: {},
    coursetalk: {},
    myReply: '',
}
}
// 删除
const onDel = id => {
    ElMessageBox.confirm(
        '真的要删除吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            axios.delete("/CourseReply/deleteCourseReplyById?id=" + id)
                .then(resp => {
                    console.log(resp.data)
                    let result = resp.data;
                    if (result.code == 1) {
                        getList();
                        ElMessage({
                            message: '删除成功！',
                            type: 'success',
                        })
                    } else {
                        ElMessage({
                            message: '删除失败！',
                            type: 'error',
                        })
                    }
                })
        })
        .catch(() => { })
}
onMounted(() => {
    console.log('onMounted')
    getList();
})
const queryInfo = ref({
    pageNum: 1,
    pageSize: 5,
    id: '',
    replyText: '',
    createTime: '',
    myReply: '',
    title: '',
    nickName: '',
})
const total = ref(0);
const tableData = ref([]);
//获取列表
const getList = () => {
    axios.get("/CourseReply/selectCourseReplyAll",
        {
            params: queryInfo.value
        })
        .then(resp => {
            console.log(resp.data.data.list)
            if (resp.data.code == 1) {
                total.value = resp.data.data.total
                tableData.value = resp.data.data.list
            } else {
                ElMessage({
                    message: "查询失败！",
                    type: 'error',
                })
            }
        })
}
//分页
const handleSizeChange = (val) => {
    queryInfo.value.pageSize = val;
    queryInfo.value.pageNum = 1;
    getList();
}
const handleCurrentChange = (val) => {
    queryInfo.value.pageNum = val;
    getList();
}
</script>

<style scoped></style>